/*
* 1.filterColumns计算属性获取隐藏信息进行返回
* 2.创建缓存列表data
* 3.filterColumns根据data进行值的返回
* 4.表格使用filterColumns计算属性显示
* 5.列显示控件列名国际化
* */
import store from "../store"

const mixin = {
    data () {
        return {
            hsColumns: [],
            searchHt: 0,
            current: 1,
            pageSize: 10,
            list: [],
            total: 0,
            queryPar: {},
            queryUrl: '',
            deleteUrl: '',
            addUrl: '',
            loading: false,
            headerHt: 82,    // 首页头部默认高度
            summaryHt: 50,   // 合计行默认高度
            showSummary: 0,  // 开启显示才能计算合计行高度
            sumText: '合计',
            sumTextNum: 0,
            selectIds: [],   // 选中的id
            hideColumnFlag: false
        }
    },
    methods: {
        setColumn(data) {
            this.hideColumnFlag = true
            this.hsColumns = data
        },
        pageChange(val) {
            this.queryPar.currentPage = val;
            this.current = val
            this.initTable()
        },
        pageSizeChange(val) {
            this.queryPar.pageSize = val;
            this.pageSize = val
            this.initTable()
        },
        remove(id){
            this.$Modal.confirm({
                title: this.$t("alarm"),
                content: '<p>' + this.$t("delAlarm") + '</p>',
                onOk: () => {
                    this.deleteData(id)
                    // this.$Message.info('Clicked ok');
                },
                onCancel: () => {
                    this.$Message.info('Clicked cancel');
                }
            });
        },
        deleteData(id) {
            const data = {'id': id}
            this.$http.request({
                url: this.deleteUrl,
                method: 'post',
                data
            }).then(res => {
                this.queryPar.currentPage = 1;
                this.initTable()
                this.$Message.info('Clicked ok');
            })
        },
        initTable() {
            this.loading = true
            const data = this.queryPar
            this.$http.request({
                url: this.queryUrl,
                method: 'post',
                data
            }).then(res => {
                let pageData = res.data
                this.list = pageData.data
                this.total = pageData.total
                this.loading = false
            })
        },
        handleSummary ({ columns, data }) {
            const sums = {};
            columns.forEach((column, index) => {
                const key = column.key;
                if (index === this.sumTextNum) {
                    sums[key] = {
                        key,
                        value: this.sumText
                    };
                    return;
                }
                const values = data.map(item => Number(item[key]));
                if (!values.every(value => isNaN(value))) {
                    const v = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return prev + curr;
                        } else {
                            return prev;
                        }
                    }, 0);
                    sums[key] = {
                        key,
                        value: v
                    };
                } else {
                    sums[key] = {
                        key,
                        value: ''
                    };
                }
            });
            return sums;
        },
        getParam() {

        },
        select(selection,row){

        },
        selectAll(selection){

        },
        selectionChange(selection) {
            this.selectIds = []
            selection.forEach((item) => {
                this.selectIds.push(item.id)
            })
        }
    },
    created: function () {
        this.initTable()
    },
    computed:{
        tableHeight() {
            if (this.showSummary === 0) {
                this.summaryHt = 0;
            }
            return store.state.app.contentHeight - this.searchHt - this.headerHt - this.summaryHt
        },
        filterColumns() {
            // 获取页面列表
            let pageColumns = this.columns1
            // 获取缓存列表
            let cacheColumns = this.hsColumns
            const filterColumns = [];
            pageColumns.forEach(item => {
                if (!this.hideColumnFlag) {
                    filterColumns.push(item);
                } else {
                    let col = cacheColumns.filter(cs => cs.key === item.key && !cs.hidden && cs.key !== undefined)
                    if (col.length > 0 /*|| cacheColumns.length===0*/) {
                        filterColumns.push(item);
                    }
                    if (item.key === undefined && item.slot !== '') {
                        filterColumns.push(item);
                    }
                }
            });
            return filterColumns
        }
    },
    mounted() {
        let searchDiv = document.getElementsByClassName('search-con')[0];
        if (searchDiv !== undefined) {
            this.searchHt = searchDiv.offsetHeight;
        }
    }
}

export { mixin }